********************************************************************************
** Do file: g4_endline_educ_hh_full.do
** First started: March 30, 2023 
** Last edited: December 31, 2023

/* Purpose: this do file analyzes education outcome variables from the endline survey.
This do file further also includes households where there are zero school aged children

(Modified) code to create indices comes from Casey et al. (2012)

* ITEM MADE: TABLE A.14
*/

********************************************************************************

clear
clear matrix
clear mata
set maxvar 10000

** Preamble - data management for both the individual level dataset and household level dataset **

* For later use: Develop a num_520 variable but for endline
use "$esvy_clean/2_educ_indiv.dta", clear
g num_520e = 1
collapse (sum) num_520e, by(hhid)
tempfile num_520e_info
save `num_520e_info'

******

** Household **

* Load key data first
use "$merged/key_rep.dta", clear
keep if hhid!=.

* Restrict down to sample of interest
keep if k_complete_may==1 & k_rolling_list==1 & k_interacted_success==1 & k_surveyed==1 & k_surveyed_end==1

* Endline		
merge 1:1 hhid using "$esvy_clean/2_educ_hh.dta"
keep if _merge==3 | _merge==1
drop _merge
		
/* Change to USD (2019) https://data.worldbank.org/indicator/PA.NUS.FCRF?locations=UG */
replace schoolexpend_t2_fin = schoolexpend_t2_fin/3704
		
* Take log
g ln_schoolexpend_t2_fin=ln(schoolexpend_t2_fin+1)
	
* Drop the choice group
drop if treatmenttype_sh=="R T3"

* Develop indicators
g anytreat_assigned = (treatmenttype_sh=="R T1-L" | treatmenttype_sh=="R T1-U" | treatmenttype_sh=="R T2-U") 
g locked_assigned = (treatmenttype_sh=="R T1-L")
g surprise_assigned = (treatmenttype_sh=="R T1-U")
g unlocked_assigned = (treatmenttype_sh=="R T2-U")

*****************************************************************************************************

* Merge in num_520e
merge 1:1 hhid using `num_520e_info', keepusing(num_520e)
replace num_520e = 0 if num_520e==.
keep if _merge==3 | _merge==1
drop _merge



************
** Part I **
************

replace ln_schoolexpend_t2_fin=0 if num_520e==0
replace schoolexpend_t2_fin=0 if num_520e==0
replace missed_month_t2_fin=30 if num_520e==0
replace enroll_t2_fin = 0 if num_520e==0

*** ITT

* Input zeroes if missing for days absent, zeroes anything else
count if missed_month_t2_fin==. & anytreat_assigned!=. & enroll_t2_fin!=. // 58
count if ln_schoolexpend_t2_fin==. & anytreat_assigned!=. & enroll_t2_fin!=. // 58
replace missed_month_t2_fin=30 if anytreat_assigned!=. & enroll_t2_fin!=. & missed_month_t2_fin==.
replace schoolexpend_t2_fin=0 if anytreat_assigned!=. & enroll_t2_fin!=. & ln_schoolexpend_t2_fin==.
replace ln_schoolexpend_t2_fin=0 if anytreat_assigned!=. & enroll_t2_fin!=. & ln_schoolexpend_t2_fin==.


g missed_month_t2_fin2 = -missed_month_t2_fin
		
local p_me_corevars_1 "enroll_t2_fin missed_month_t2_fin2 ln_schoolexpend_t2_fin"

* Cut to the appropriate sample
keep if enroll_t2_fin !=.

foreach num of numlist 1 {
local i = `num'
	* 1. Convert outcomes to effect sizes
	foreach var of varlist  `p_me_corevars_`i'' {
		local l = `l' + 1
		qui sum `var' if anytreat_assigned==0
		qui gen `var'_mean=r(mean)
		qui gen `var'_sd=r(sd)
		qui gen std_`i'_`l'=(`var'-`var'_mean)/`var'_sd
		qui egen mean_std_`i'_`l'_t=mean( std_`i'_`l') if anytreat_assigned==1 
		qui replace std_`i'_`l'=mean_std_`i'_`l'_t if anytreat_assigned==1 & std_`i'_`l'==.
		qui replace std_`i'_`l'=0 if anytreat_assigned==0 & std_`i'_`l'==.
		local matrix_string_`i'="`matrix_string_`i''" + " std_`i'_`l'"
		}
		local k = `l'
		save "$temp/mei_std_h`i'.dta", replace
		
	* 2. Construct matrix and component weights using controls only;
	drop if anytreat_assigned==1
	
		forvalues x = 1/`k' {
			gen weight`x'_`i'=0
			}
	
		matrix accum R = `matrix_string_`i'', nocons dev
		matrix R=R/r(N)
		matrix R=invsym(R)
		local counter1=1
		matrix J = J(colsof(R), 1, 1)
		while `counter1'<=colsof(R){
			matrix T = R[`counter1', 1..colsof(R)]
			matrix A = T*J
			qui replace weight`counter1'_`i'=A[1,1]
			qui replace weight`counter1'_`i'=0 if weight`counter1'_`i'<0
			local counter1 = `counter1'+1
			}
			
		gen sample_h`i'=0
		forvalues x = 1/`k' {
			qui replace sample_h`i'=sample_h`i'+weight`x'_`i'
			}
		keep sample_h`i' weight*
		collapse _all
				
		cross using "$temp/mei_std_h`i'.dta"
			
	* 3. Apply weights and construct index;
		qui gen outcome_h`i'=0
			forvalues x = 1/`k' {
			qui replace outcome_h`i'=std_`i'_`x'*(weight`x'_`i') + outcome_h`i'
			}
			qui replace outcome_h`i'=outcome_h`i'/sample_h`i'
		local l = 0
		drop *_mean *_sd mean_std_*
}
	* 4. Estimate treatment effects;
		foreach num of numlist 1 {
			reg outcome_h`num' anytreat_assigned
			estimates store ander_`num'
			}

* Generate interaction term with num_520e
g atreatnum_520e = anytreat_assigned * num_520e

***** TABLE A.14 *****

* Outcome means

sum enroll_t2_fin if anytreat_assigned!=. & anytreat_assigned==0
sum missed_month_t2_fin if anytreat_assigned!=. & anytreat_assigned==0
sum schoolexpend_t2_fin if anytreat_assigned!=. & anytreat_assigned==0
sum outcome_h1 if anytreat_assigned!=. & anytreat_assigned==0

reg enroll_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

reg missed_month_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

reg ln_schoolexpend_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

reg outcome_h1 locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e
test locked_assigned = unlocked_assigned

* OLS results

eststo: reg enroll_t2_fin anytreat_assigned atreatnum_520e num_520e
eststo: reg enroll_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

eststo: reg missed_month_t2_fin anytreat_assigned atreatnum_520e num_520e
eststo: reg missed_month_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

eststo: reg ln_schoolexpend_t2_fin anytreat_assigned atreatnum_520e num_520e
eststo: reg ln_schoolexpend_t2_fin locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

eststo: reg outcome_h1 anytreat_assigned atreatnum_520e num_520e
eststo: reg outcome_h1 locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e

esttab using "$tables/endline_educ_hh_full.tex", ///
	b(2) se(2) se replace booktabs star(* .10 ** .05 *** .01) title(Education Outcomes, Household-level, Including Households without School-Aged Children\label{endline_educ_hh_full}) ///
	label nodepvars mtitles("Enrollment" "Enrollment" "Days absent" "Days absent" "Log school expenditures" "Log school expenditures" "Education index" "Education index") nonotes ///
	order(anytreat_assigned locked_assigned surprise_assigned unlocked_assigned atreatnum_520e num_520e _cons) ///
	varlabels(anytreat_assigned "Pooled" locked_assigned "Secured" surprise_assigned "Surprise Unsecured" unlocked_assigned "Unsecured" _cons "Constant" num_520e "N SAC at endline" atreatnum_520e "Pooled $\times$ Number of School-Aged Children", ///
	elist(anytreat_assigned \addlinespace locked_assigned \addlinespace surprise_assigned \addlinespace unlocked_assigned \addlinespace atreatnum_520e \addlinespace num_520e \addlinespace)) 
eststo clear
